package gok.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.impl.McTypeDAOImpl;
import net.sf.json.JSONArray;
import bean.McTypeBean;
import oracle.net.aso.r;

public class McTypeServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String task = request.getParameter("task");
		McTypeDAOImpl typeCRUDImpl = new McTypeDAOImpl();
		PrintWriter out = response.getWriter();
		if ("del".equals(task)) {
			int nid = Integer.parseInt(request.getParameter("nid"));
			McTypeBean typeBean = new McTypeBean(nid, null, 0);
			boolean flag = typeCRUDImpl.deleteMcType(typeBean);
			if (flag) {
//				JSONArray json = JSONArray.fromObject(typeCRUDImpl
//						.queryAllMcType());
//				System.out.println("jsonservlet="+json);
//				out.println(json);
//				
//				System.out.println("json"+json);
				out.println("true");
			}
			out.println("false");
		} else if ("update".equals(task)) {
			int nid = Integer.parseInt(request.getParameter("nid"));
			String sname = request.getParameter("sname");
			int npid = Integer.parseInt(request.getParameter("npid"));
			McTypeBean typeBean = new McTypeBean(nid, sname, npid);
			boolean flag = typeCRUDImpl.updateMcType(typeBean);
			if (flag) {
				// JSONArray json = JSONArray.fromObject(typeCRUDImpl
				// .queryAllMcType());
				// out.println(json);
				out.println("true");
			}
			out.println("false");
			out.println("resopnse.setHeader('refresh')");
		}else if("add".equals(task)){
			int nid = typeCRUDImpl.queryMaxNid();
			String sname = request.getParameter("sname");
			int npid = Integer.parseInt(request.getParameter("npid"));
			McTypeBean typeBean = new McTypeBean(nid, sname, npid);
			System.out.println("addType="+typeBean);
			boolean flag = typeCRUDImpl.insertMcType(typeBean);
			if(flag){
				response.sendRedirect("admin/adminType.jsp");
			}else{
				response.sendRedirect("admin/addType.jsp");
			}
		}else if("queryNid".equals(task)){
			
		}else if("querySname".equals(task)){
			
		}else if("queryNpid".equals(task)){
			
		}
	}
}
